﻿using System.Collections.Generic;
using MSP.Data.Domain;
using MSP.Data.NHibernate;
using MSP.Data.Repositories;
using MSP.Business.Logic;
using MSP.Data.Domain.Enums;

namespace MSP.Business.ProductManagement
{
    public class ProductActionManager
    {
        public int SaveProductAction(ProductAction productAction)
        {
            int productProductActionId = 0;

            if (productAction != null)
            {
                using (var unitOfWork = new UnitOfWork(NHibernateSessionFactory.SessionFactory))
                {
                    var productActionRepository = new ProductActionRepository(unitOfWork.Session);
                    productActionRepository.Save(productAction);
                    unitOfWork.Commit();
                }

                productProductActionId = productAction.Id;
            }

            return productProductActionId;
        }

        public int SaveOrUpdate(ProductAction productAction)
        {
            int productProductActionId = 0;

            if (productAction != null)
            {
                using (var unitOfWork = new UnitOfWork(NHibernateSessionFactory.SessionFactory))
                {
                    var productActionRepository = new ProductActionRepository(unitOfWork.Session);
                    productActionRepository.SaveOrUpdateCopy(productAction);
                    unitOfWork.Commit();
                }

                productProductActionId = productAction.Id;
            }

            return productProductActionId;
        }

        public bool UpdateProductAction(ProductAction productAction)
        {
            bool result = false;

            using (var unitOfWork = new UnitOfWork(NHibernateSessionFactory.SessionFactory))
            {
                var productActionRepository = new ProductActionRepository(unitOfWork.Session);
                result = productActionRepository.Update(productAction);
                unitOfWork.Commit();
            }

            return result;
        }

        public bool DeleteProductAction(ProductAction productAction)
        {
            bool result = false;
            using (var unitOfWork = new UnitOfWork(NHibernateSessionFactory.SessionFactory))
            {
                var productActionRepository = new ProductActionRepository(unitOfWork.Session);
                result = productActionRepository.Delete(productAction);
                unitOfWork.Commit();
            }

            return result;
        }

        public ProductAction GetProductActionById(int Id)
        {
            var productActionRepository = new ProductActionRepository(NHibernateSessionFactory.SessionFactory.OpenSession());
            ProductAction productAction = productActionRepository.GetProductActionById(Id);
            return productAction;
        }

        public IList<ProductAction> GetAllProductAction()
        {
            var productActionRepository = new ProductActionRepository(NHibernateSessionFactory.SessionFactory.OpenSession());
            IList<ProductAction> productActions = productActionRepository.GetAllProductAction();

            return productActions;
        }

        public IList<KeyValuePair> GetAllProductActionType()
        {
            return KeyValuePair.ListFrom<ProductActionTypeEnum>();
        }

    }
}
